﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Newtonsoft.Json;

namespace Entropy.Ext
{
	public class StoreResult: ContentResult
	{
		public StoreResult(object data, string message = null, int? totalRecords = null, bool? success = null) 
		{
			var json = JsonConvert.SerializeObject(new ExtStoreViewModel
			{
				Root = data,
				Message = message,
				TotalRecords = totalRecords,
				Success = success
			});
			// todo make some traffic optimizations: marshal props as arrays and/or gzip it

			Content = json;
			ContentType = null;
			ContentEncoding = null;
		}
	}

	public class StoreResult<TViewModel> : StoreResult
	{
		public StoreResult(IEnumerable<TViewModel> data, string message = null, int? totalRecords = null, bool? success = null)
			: base(
			data.ToArray(), // todo optimize memory usage - dont use arrays here
			message, 
			totalRecords, 
			success)
		{
		}
	}


}